﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_user_add : System.Web.UI.Page
{

    Cstudy.Tools.RegexTools regextools = new Cstudy.Tools.RegexTools();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void NameBox_TextChanged(object sender, EventArgs e)
    {
        string name = NameBox.Text.Trim();
        if (name.Length < 4)
        {
            vname.Text = "用户名至少4位";
            return;
        }
        if (new Cstudy.BLL.Users().ExistUserName(name))
        {
            vname.Text = "用户名已存在";
        }
        else
        {
            vname.Text = "可以注册";
        }
    }
    protected void PwdBox_TextChanged(object sender, EventArgs e)
    {
        if (regextools.IsValidByte(PwdBox.Text, 5, 15))
        {
            pwdlit.Text = "密码格式正确";
        }
        else
        {
            pwdlit.Text = "密码必须是6到15位且非特殊字符";
        }
    }
    protected void rPwdBox_TextChanged(object sender, EventArgs e)
    {
        if (PwdBox.Text != rPwdBox.Text)
        {
            vpwd.Text = "重复密码不正确！";
        }
        else
        {
            vpwd.Text = "重复密码正确！";
        }
    }
    protected void EmaiBox_TextChanged(object sender, EventArgs e)
    {
        if (regextools.IsValidEmail(EmaiBox.Text))
        {
            emaillit.Text = "Email地址正确";
        }
        else
        {
            emaillit.Text = "Email地址不正确";
        }
        if (new Cstudy.BLL.Users().ExitsEmail(EmaiBox.Text))
        {
            emaillit.Text = "Email已存在";
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (CodeBox.Text == GetCode())
        {
            validatelit.Text = "验证码正确";
        }
        else
        {
            validatelit.Text = "验证码不正确";
        }
    }

    protected string GetCode()
    {
        return Session["AuthenticationCode"].ToString();
    }

    protected bool CanRegister()
    {
        bool can = true;
        if (new Cstudy.BLL.Users().ExistUserName(NameBox.Text))
        {
            can = false;
        }
        if (PwdBox.Text != rPwdBox.Text)
        {
            can = false;
        }
        if (!((new Cstudy.Tools.RegexTools().IsValidEmail(EmaiBox.Text)) && (new Cstudy.BLL.Users().ExitsEmail(EmaiBox.Text))))
        {
            can = false;
        }
        if (CodeBox.Text != GetCode())
        {
            can = false;
        }
        return can;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (CanRegister())
        {
            Cstudy.MODEL.users user = new Cstudy.MODEL.users();
            user.UserName = NameBox.Text;
            user.Pwd = new Cstudy.Tools.Tools().MakeMD5(PwdBox.Text);
            user.Email = EmaiBox.Text;
            if (new Cstudy.BLL.Users().AddUser(user))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "successalert", "alert('用户添加成功！');window.location.href='Login.aspx';", true);
                return;
            }
        }
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "failalert", "alert('用户添加失败！');", true);
    }
}